package com.zte.coding.measure;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 14-3-5
 * Time: 下午7:35
 * To change this template use File | Settings | File Templates.
 */
public class Volume {
    private int value;

    public Volume(int num, VolumeUnit unit) {
        this.value = num * unit.getRate();
    }

    @Override
    public boolean equals(Object obj) {
        if (null == obj) {
            return false;
        }

        if (this == obj) {
            return true;
        }

        //保证对象的类型强转正确
        if (obj instanceof Volume) {
            if (Double.doubleToLongBits(this.value) == Double.doubleToLongBits(((Volume) obj).value)) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    public Volume add(Volume volume) throws Exception {
        if (null == volume) {
            throw new Exception();
        }

        return new Volume(this.value + volume.value, VolumeUnit.TSP);
    }
}
